package cn.wolfcde.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.ScenicQuery;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQueryObject;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IRegionService regionService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IScenicService scenicService;

    @Reference
    private IScenicContentService scenicContentService;

    /**
     * 目的地首页
     */
    @RequestMapping("")
    public String queryHotRegion(Model model) {

        List<Region> hotRegions = regionService.queryHotRegion();

        model.addAttribute("hotRegions", hotRegions);

        return "destination/index";

    }


    /**
     * 热门目的地
     *
     * @param model
     * @param regionId
     * @return
     */
    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, @ModelAttribute("regionId") Long regionId) {

        List<Destination> destinations = regionService.getHotDestByRegionId(regionId);

        int size = destinations.size();

        List<Destination> leftDests = destinations.subList(0, size / 2 + 1);

        List<Destination> rightDests = destinations.subList(size / 2 + 1, size);

        model.addAttribute("leftDests", leftDests);

        model.addAttribute("rightDests", rightDests);


        return "destination/hotdestTpl";

    }

    /**
     * 目的地明细页面
     *
     * @param id 目的地ID
     * @return
     */
    @RequestMapping("/guide")
    public String guide(Long id, Model model) {


        List<Destination> toasts = destinationService.getToasts(id);

        //最后一个数据被页面做过特殊处理,删去 返回数据
        Destination destination = toasts.remove(toasts.size() - 1);

        model.addAttribute("dest", destination);

        model.addAttribute("toasts", toasts);

        //catalogs
        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.getCatalogByDestId(id);

        model.addAttribute("catalogs", strategyCatalogs);

        //strategyDetails

        List<StrategyDetail> strategyDetailsTop3 = strategyDetailService.getDetailsByViewTop3(id);

        model.addAttribute("strategyDetails", strategyDetailsTop3);

        return "destination/guide";

    }

    /**
     * 目的地页面
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/surveyPage")
    public String surveyPage(@ModelAttribute("qo") StrategyCatalogQuery qo, Model model) {

        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        toasts.remove(toasts.size() - 1);
        //toast
        model.addAttribute("toasts", toasts);

        //dest
        Destination destination = destinationService.selectById(qo.getDestId());
        model.addAttribute("dest", destination);

        return "destination/survey";
    }

    /**
     * 概况页面
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/survey")
    public String survey(@ModelAttribute("qo") StrategyCatalogQuery qo, Model model) {

        //catalogs
        List<StrategyCatalog> catalogByDestId = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogByDestId);

        //catalog
        StrategyCatalog strategyCatalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog", strategyCatalog);

        //detail
        List<StrategyDetail> strategyDetails = strategyDetailService.queryByCatalogId(qo.getCatalogId());
        if (strategyDetails != null && strategyDetails.size() > 0) {
            model.addAttribute("detail", strategyDetails.get(0));
        }

        return "destination/surveyTpl";
    }


    @RequestMapping("/travels")
    public String survey(@ModelAttribute("qo") TravelQueryObject qo, Model model) {

//        pageInfo
        PageInfo pageInfo = travelService.queryForList(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "destination/travelTpl";
    }

    /**
     * 景点显示
     * @param model
     * @return
     */
    @RequestMapping("/scenic")
    public String scenic(@ModelAttribute("qo")ScenicQuery qo, Model model){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        toasts.remove(toasts.size() - 1);
        //dest
        Destination destination = destinationService.selectById(qo.getDestId());
        model.addAttribute("dest", destination);
        //toast
        model.addAttribute("toasts", toasts);
        List<Scenic> scenics = scenicService.replyNumTop5(qo.getDestId());
        model.addAttribute("top5",scenics);

        PageInfo<Scenic> pageInfo= scenicService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "scenic/scenic";
    }



}
